import axiosInstance, { baseURL } from '@/utils/network/axios'
import type { AxiosResponse } from 'axios'
import { url_join } from '../tools/url-join'

export interface DirectoryItem {
    type: 'file' | 'folder' | 'parent' // "file" | "dir" | "parent"
    owner: string
    path_name: string
    size?: number
    ///////////////////////
    selected?: boolean
}

export async function axg_getDirectoryList(path: string = '/'): Promise<DirectoryItem[]> {
    const response = await axiosInstance.get(`/home/directory/list?p=${encodeURIComponent(path)}`)
    return response.data
}

export function generate_download_link(path: string, filename: string): string {
    const download_path = url_join(path, filename)
    const download_url = encodeURIComponent(download_path)
    const download_link = url_join(baseURL, '/home/directory/file') + `?p=${download_url}`
    return download_link
}

export async function axp_packfile(
    files: {
        parent: string
        file_list: DirectoryItem[]
    },
    tasks: {
        task_id: string
        pack_name: string
        pack_method: string
    }
): Promise<boolean> {
    const response = await axiosInstance.post(
        '/home/directory/file-operation?action=pack',
        Object.assign(files, tasks)
    )
    if (response.status !== 200) {
        return false
    } else {
        return true
    }
}
